<?php
/**
 * Created by IntelliJ IDEA. 
 * User: cmn
 * Date: 2019-10-16
 * Time: 19:48
 */
namespace tech\esign_core;

use tech\esign_utils\CommonHelper;
use tech\esign_utils\HttpHelper;

class SignHelper extends BaseHelper
{

    public function selfFileSign($srcPdfPath,$dstPdfFile,$signType,$signPos){
        $selfSignData = array(
            "file"=>
                array(
                    "srcPdfFile"=>$srcPdfPath,  //待签署PDF文档本地路径
                    "dstPdfFile"=>$dstPdfFile, //签署后PDF文档本地路径（不传则返回签署后的文件流）
                ),
            "signType"=>$signType,  //签章类型，Single（单页签章）、Multi（多页签章）、Edges（签骑缝章）、Key（关键字签章）
            "sealId"=>0, //签署印章标识 为0 代表默认印章，测试环境统一为"天谷"印章
            "signPos"=>$signPos
        );

        $result = HttpHelper::doPost($this->warUrl.$this->urlMap['SELF_FILE_SIGN'],$selfSignData);
        return json_decode($result,true);
    }

    public function selfStreamSign($srcPath,$fileName,$dstPdfFile,$signType,$signPos){
        $srcPdfFile = HttpHelper::encodePath($srcPath);
        if (! CommonHelper::isUrl($srcPdfFile)) {
            return array(
                'errCode' => 913,
                'msg' => 'srcPdfFile 路径不是远程Url',
            );
        }
        $file = CommonHelper::getRealFileIgnore($srcPdfFile);
        $selfStreamSignData = array(
            "file"=>$file,
            "fileName"=>$fileName,
            "dstPdfFile"=>$dstPdfFile,
            "signType"=>$signType,  //签章类型，Single（单页签章）、Multi（多页签章）、Edges（签骑缝章）、Key（关键字签章）
            "sealId"=>0, //签署印章标识 为0 代表默认印章，测试环境统一为"天谷"印章
            "signPos"=>$signPos

        );
        $result =  HttpHelper::postFile($this->warUrl.$this->urlMap['SELF_STREAM_SIGN'],$selfStreamSignData);
        return json_decode($result,true);

    }

    public function userSign($srcPdfFile,$dstPdfFile,$accountId,$sealData,$signPos,$signType){
        $userSignData = array(
            "file"=>
                array(
                    "srcPdfFile"=>$srcPdfFile,  //待签署PDF文档本地路径
                    "dstPdfFile"=>$dstPdfFile, //签署后PDF文档本地路径（不传则返回签署后的文件流）
                ),
            "signType"=>$signType,  //签章类型，Single（单页签章）、Multi（多页签章）、Edges（签骑缝章）、Key（关键字签章）
            "accountId"=>$accountId,
            "sealData"=>$sealData, // 印章图片base64
            "signPos"=>$signPos
        );
        $result = HttpHelper::doPost($this->warUrl.$this->urlMap['USER_FILE_SIGN'],$userSignData);
        return json_decode($result,true);
    }

}
